package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.Table;
import com.fzrskj.cloud.free.sys.bean.Field;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.List;

public class CDicDao extends CObject{
	//private declaration------------------------------------------------
	private IDbDrv db = null;

	//constructor function to initialize database
	public CDicDao(){
		this.db = CloudDbDrvUtils.getDbUtilsDrv();
	}

	//dispose function implements to free resource
	@Override
	public void dispose(){
		if (db!=null) {
			db.closeConnection();
		}
		db = null;
		super.dispose();
	}

	// to get table definition
	public Table getTableDef(String code) {
		String strSql = String.format("select * from dic_table where code='%s'",code);
		Table ret = db.queryForBean(strSql,Table.class);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(err);
		return ret;
	}

	//get table definition by table type
	public List<Table> getTalbeDic(int iType) {
		String strSql = String.format("select * from dic_table where itype=%d",iType);
		List<Table> ret = this.db.queryForBeans(strSql,Table.class);
		String err = this.db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(this.db.getMsg());
		}
		this.setErrCode(err);
		return ret;
	}

	//get table field definition
	public List<Field> getFieldDic(String code){
		String strSql = String.format("select * from dic_field where code='%s' order by showId",code);
		List<Field> ret = db.queryForBeans(strSql, Field.class);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		setErrCode(err);
		return ret;
	}
	
	//to lock table field width and show name,return how many column width has been updated
	public int lockFieldDic(List<Field> fields){
		String strSql;
		int n=0;
		for (Field fld: fields){
			strSql = "update dic_field set width=?,showId=?,isShow=?,sname=? where code=? and name=?";
			Object[] params = {fld.getWidth(),fld.getShowId(),fld.getIsShow(),fld.getSname(), fld.getCode(),fld.getName()};
			if (db.executeSql(strSql,params)>0) {
				n++;
			}
		}
		return n;
	}

	//to truncate table data
	public int truncateTable(String code){
		int ret = -1;
		//get table name by table code
		String sql = "select ename from dic_table where code='%s'";
		Object[] params = {code};
		Table tb = db.queryForBean(sql, Table.class, params);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
			return ret;
		}

		if (tb==null){
			setErrCode(AppCode.TABLE_NOT_EXISTS);
			return ret;
		}

		//truncate table data
		sql = String.format("truncate table %s",tb.getName());
		ret = db.executeSql(sql);
		err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		setErrCode(err);
		return ret;
	}
}
